package command;

import java.util.ArrayList;
import java.util.List;

import model.impl.Schedule;
import model.impl.ScheduleImpl;
import DTO.DTOManager;
import DTO.ScheduleDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;


/**
 * Class represents command interface;
 * It sends all the schedules objects to the administer user. To send them 
 * it uses DTO. If there is any schedules it sends message OK and the list
 * of schedules, else sends no and null.
 */
public class AdminManageSchedule implements Command {
	
	private Protocol protocol = new Protocol();
	
	@Override
	public void execute(ReceivedData recived) {
		model.Schedule scheduleImpl = new ScheduleImpl();
		List<Schedule> schedules = scheduleImpl.findAll();
		List<ScheduleDTO> schedulesDTO = new ArrayList<ScheduleDTO>();
		if (schedules.isEmpty() || (schedules == null)) {
			Protocol.Message answer = this.protocol.new Message("no", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		} else {
			for(int i = 0; i < schedules.size(); i++) {
				schedulesDTO.add((new DTOManager()).createScheduleDTO(schedules.get(i)));
			}
			Protocol.Message answer = this.protocol.new Message("ok", schedulesDTO);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		}
	}
}
